﻿using mscook.core.domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Linq;
namespace mscook.core.repositories
{
    public class MagazineRepository : Repository<Magazine>,IMagazineRepository
    {
        public Magazine GetByYearMonth(string year, string month)
        {
            var query = (from x in this.Session.Query<Magazine>() select x)
             .Where(o => o.IsDelete == false
                 && o.Year == year
                 && o.Volume == month
                 );
            return query.SingleOrDefault();
        }

        public IList<Magazine> GetRecent(int start,int limit)
        {
            var query = (from x in this.Session.Query<Magazine>() select x)
            .Where(o => o.IsDelete == false
                ).OrderByDescending(o=>o.EntityIndex).OrderByDescending(o=>o.CreateTime);
            return query.Skip(start).Take(limit).ToList();
        }

       

        public IList<Magazine> GetOrderByYearMonth(int start, int limit,out long total)
        {
            var query = (from x in this.Session.Query<Magazine>() select x)
          .Where(o => o.IsDelete == false
              ).OrderByDescending(o => o.Year).OrderByDescending(o => o.Volume).OrderByDescending(o=>o.CreateTime);

            total = query.Count();
            return query.Skip(start).Take(limit).ToList();
        }

        public IList<Magazine> SelectPageList(int start, int limit, out long total, string title)
        {
            var query = (from x in this.Session.Query<Magazine>() select x)
               .Where(o => o.IsDelete == false
                   && (string.IsNullOrEmpty(title) || o.Title.Contains(title))
               ).OrderByDescending(o => o.EntityIndex)
               .OrderByDescending(o => o.CreateTime);
            total = query.LongCount();

            if (start > total)
            {
                int tempStart = Int32.Parse(total.ToString()) - limit;
                return query.Skip(tempStart).Take(limit).ToList<Magazine>();
            }
            return query.Skip(start).Take(limit).ToList<Magazine>();
        }
    }
}
